package com.czh.springboot04.service.serviceimpl;

import com.czh.springboot04.entity.Charge;
import com.czh.springboot04.entity.ChargeType;
import com.czh.springboot04.entity.Consult;
import com.czh.springboot04.mapper.ChargeMapper;
import com.czh.springboot04.mapper.ChargeTypeMapper;
import com.czh.springboot04.mapper.ConsultMapper;
import com.czh.springboot04.service.ChargeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ChargeServiceImpl implements ChargeService {
    @Autowired
    private ChargeMapper chargeMapper;
    @Autowired
    private ChargeTypeMapper chargeTypeMapper;
    @Autowired
    private ConsultMapper consultMapper;
    @Override
    public int insert(Charge charge) {
        int i = chargeMapper.insert(charge);
        return i;
    }

    @Override
    public int update(Charge charge) {
        return chargeMapper.update(charge);
    }
    @Override
    public int delete(int id) {
        int i = chargeMapper.delete(id);
        return i;
    }

    @Override
    public PageInfo<Charge> selectByPage(int pageNum, int pageSize, Charge c) {
        PageHelper.startPage(pageNum,pageSize);
        List<Charge> charges = chargeMapper.selectByPage(c);
        for (Charge charge : charges) {
            Consult consult = consultMapper.findConsultById(charge.getConsultId());
            charge.setConsult(consult);
            ChargeType chargeType = chargeTypeMapper.selectById(charge.getTypeId());
            System.out.println(chargeType);
            charge.setChargeType(chargeType);
        }
        return new PageInfo<>(charges);
    }
    @Override
    public Charge selectById(int id) {
        return chargeMapper.selectById(id);
    }

    /**
     * @return
     */
    @Override
    public List<Charge> selectAll() {
        List<Charge> charges = chargeMapper.selectAll();
        for (Charge charge : charges) {
            Consult consult = consultMapper.findConsultById(charge.getConsultId());
            charge.setConsult(consult);
            ChargeType chargeType = chargeTypeMapper.selectById(charge.getTypeId());
            System.out.println(chargeType);
            charge.setChargeType(chargeType);
        }
        return charges;
    }
}